<template>
  <div class="app-container">
    <!--    <el-form :inline="true" :model="searchFrom" class="demo-form-inline" size="small">-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.number" placeholder="用户编号"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-input v-model="searchFrom.name" placeholder="用户名称"/>-->
    <!--      </el-form-item>-->
    <!--      <el-form-item>-->
    <!--        <el-button type="primary" @click="onSearch">查询</el-button>-->
    <!--      </el-form-item>-->
    <!--    </el-form>-->
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="110">
        <template slot-scope="scope">
          {{ scope.row.realId }}
        </template>
      </el-table-column>
      <el-table-column label="用户头像" align="center" width="110">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="40" fit="fit" :src="scope.row.avatar"/>
        </template>
      </el-table-column>
      <el-table-column label="昵称" align="center">
        <template slot-scope="scope">
          {{ scope.row.nickname }}
        </template>
      </el-table-column>
      <el-table-column label="真实实名" align="center">
        <template slot-scope="scope">
          {{ scope.row.realName }}
        </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center">
        <template slot-scope="scope">
          {{ scope.row.realCode }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="进度" align="center" width="110">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.realProgress | progressFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="失败原因" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.realReply ? scope.row.realReply : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="认证时间">
        <template slot-scope="scope">
          <i class="el-icon-time"/>
          <span>{{ scope.row.realCreateDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="基本操作" width="180">
        <template slot-scope="scope">
          <el-button v-if="scope.row.realProgress === 10" type="primary" size="mini" @click="confirm(scope.row,1)">通过
          </el-button>
          <el-button v-if="scope.row.realProgress === 10" type="primary" size="mini" @click="confirm(scope.row,2)">拒绝
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="app-pagination">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @prev-click="handlePrevChange"
        @next-click="handleNextChange"
      />
    </div>
  </div>
</template>

<script>

import { getUmsRealPage, updateUmsReal } from '@/api/UmsReal'

export default {
  filters: {
    progressFilter(status) {
      const statusMap = {
        '0': '未审核',
        '10': '认证中',
        '20': '认证成功',
        '30': '认证失败'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      searchFrom: {
        name: '',
        number: '',
        email: ''
      },
      list: null,
      listLoading: true,
      total: 0,
      limit: 10,
      page: 1
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const searchParams = this.searchFrom
      searchParams['page'] = this.page
      searchParams['limit'] = this.limit
      getUmsRealPage(searchParams).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    onSearch() {
      this.fetchData()
    },
    /* 上一页*/
    handlePrevChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 下一页*/
    handleNextChange(event) {
      this.page = event
      this.fetchData()
    },
    /* 数字点击*/
    handleCurrentChange(event) {
      this.page = event
      this.fetchData()
    },
    handleSizeChange(event) {
      this.limit = event
      this.fetchData()
    },
    confirm(event, type) {
      const realForm = {
        realId: event.realId,
        realReply: null,
        realProgress: null
      }
      if (type === 1) {
        realForm.realProgress = 20
        updateUmsReal(realForm).then(res => {
          if (res.code === 200) {
            this.$message.success('通过成功')
            this.fetchData()
          } else {
            this.$message.error('网络错误')
          }
        })
      } else {
        realForm.realProgress = 30
        this.$prompt('请输入拒绝理由', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          realForm.realReply = value
          updateUmsReal(realForm).then(res => {
            if (res.code === 200) {
              this.$message.success('已拒绝')
              this.fetchData()
            } else {
              this.$message.error('网络错误')
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      }
    }
  }
}
</script>
